package com.sg.java.util;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class BJCsvUtils {


    public static HashMap<String, String> CsvToObject(String filePath, String[] headers, String fileName) throws Exception {


        String file = findFile(filePath, fileName);


        HashMap<String, String> hashMap = new HashMap<>();
        //创建CSVFormat
        CSVFormat formator = CSVFormat.DEFAULT.withHeader(headers);

        FileReader fileReader = null;
        CSVParser parser = null;
        int num = 0;
        try {
            fileReader = new FileReader(file);
            parser     = new CSVParser(fileReader, formator);
            for (CSVRecord next : parser) {
                if (ObjectUtil.isNull(next) || next.size() < 5) {
                    continue;
                }
                String str = next.get(0) + "!@@" + next.get(1) + "!@@" + next.get(3) + "!@@" + next.get(4);
                hashMap.put(next.get(2), str);


            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                parser.close();
                fileReader.close();


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return hashMap;
    }

    public static HashMap<String, String> CsvToObject2(String filePath, String[] headers, String month, String fileName, Map<String, Void> METER_IDS) throws Exception {

//        Jedis jedis = JedisUtils.getJedis();
//        Pipeline jp = jedis.pipelined();

        String file = findFile(filePath, fileName);


        HashMap<String, String> hashMap = new HashMap<>();
        //创建CSVFormat
        CSVFormat formator = CSVFormat.DEFAULT.withHeader(headers);

        FileReader fileReader = null;
        CSVParser parser = null;
        int num = 0;
        try {
            fileReader = new FileReader(file);
            parser     = new CSVParser(fileReader, formator);
            for (CSVRecord next : parser) {
                if (ObjectUtil.isNull(next) || next.size() < 5 || (METER_IDS != null && !METER_IDS.containsKey(next.get(2)))) {
                    continue;
                }

                String str = next.get(0) + "!@@" + next.get(1) + "!@@" + next.get(3) + "!@@" + next.get(4);

                hashMap.put(next.get(2), str);
//                }


//                String ctKey = "CT@" + month + "@" + next.get(3) + "@" + VoltageLevelEnum.valueOf(String.valueOf(next.get(1))).getCode();
//                jp.sadd(ctKey, next.get(2));
//                jedis.sadd(ctKey, next.get(2));

//                num++;
//                if (num > 10000) {
//                    jp.sync();
//                    num = 0;
//                }
            }

//            jp.sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                parser.close();
                fileReader.close();
//                jp.close();
//                jedis.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return hashMap;
    }


    public static HashMap<String, String> CsvToPb(String filePath, String[] headers, String fileName) throws Exception {


        String file = findFile(filePath, fileName);

        HashMap<String, String> hashMap = new HashMap<>();
        //创建CSVFormat
        CSVFormat formator = CSVFormat.DEFAULT.withHeader(headers);

        FileReader fileReader = null;
        CSVParser parser = null;
        try {
            fileReader = new FileReader(file);
            parser     = new CSVParser(fileReader, formator);
            for (CSVRecord next : parser) {
                if (ObjectUtil.isNull(next) || next.size() < 2) {
                    continue;
                }
                hashMap.put(next.get(0), next.get(1));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                parser.close();
                fileReader.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return hashMap;
    }


    public static HashMap<String, String> CsvToPbCd(String filePath, String[] headers, String fileName) throws Exception {


        String file = findFile(filePath, fileName);
        HashMap<String, String> hashMap = new HashMap<>();
        //创建CSVFormat
        CSVFormat formator = CSVFormat.DEFAULT.withHeader(headers);

        FileReader fileReader = null;
        CSVParser parser = null;
        int num = 0;
        try {
            fileReader = new FileReader(file);
            parser     = new CSVParser(fileReader, formator);
            for (CSVRecord next : parser) {
                if ("".equals(next.get(3))) {
                    continue;
                }
                //全部的配变都先按找220V来算
                String s = next.get(1) + "!@@" + "AC02202" + "!@@" + next.get(3) + "!@@" + next.get(4);
                hashMap.put(next.get(0), s);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                parser.close();
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return hashMap;
    }


    public static String findFile(String filePath, String zpbyhName) throws Exception {
        File file = new File(filePath);
        String[] names = file.list();
        for (String name : names) {
            boolean a = name.startsWith(zpbyhName);
            if (a) {
                return filePath + name;
            }
        }

        throw new Exception("文件名有误");
    }


    public static HashMap<String, String> ratedCapacity(String pbFilePath, String[] pbHeaders, String zFileName) throws Exception {

        String file = findFile(pbFilePath, zFileName);


        HashMap<String, String> hashMap = new HashMap<>();
        //创建CSVFormat
        CSVFormat formator = CSVFormat.DEFAULT.withHeader(pbHeaders);

        FileReader fileReader = null;
        CSVParser parser = null;
        try {
            fileReader = new FileReader(file);
            parser     = new CSVParser(fileReader, formator);
            for (CSVRecord next : parser) {
//                if ("".equals(next.get(3))) {
//                    continue;
//                }
                String s = next.get(0);
                String[] split = s.split(",");
                hashMap.put(next.get(1), next.get(5));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                parser.close();
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return hashMap;
    }
}
